﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_EShop._Class;
using System.IO;

namespace V5_EShop.Admin.Plus.Picture
{
    public partial class Main : System.Web.UI.Page
    {
        protected string backHtml;
        protected string folderPath = HttpContext.Current.Request.QueryString["path"];
        protected System.Text.StringBuilder currPath = new System.Text.StringBuilder("");

        protected int folderNum = 0;
        protected int fileNum = 0;

        protected System.Text.StringBuilder builder = new System.Text.StringBuilder("");

        public string rootPath = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            rootPath = Server.MapPath("~/");
            // 操作处理
            FileManagerProcessor fileManage = new FileManagerProcessor(Request.QueryString["act"]);
            builder.Append(fileManage.Value);

            if (string.IsNullOrEmpty(folderPath))
            {
                folderPath = Server.MapPath("~/UploadFiles/");
            }

            // 组合路径, 快速导航
            string comePath = "";
            foreach (string q in folderPath.Split('\\'))
            {
                comePath += q;
                currPath.AppendFormat("<a href=\"Main.aspx?path={1}\">{0}</a>", q + "\\", comePath);
                comePath += "\\";
            }

            // 返回上级
            if (new DirectoryInfo(folderPath).Root.ToString().Replace("\\", "") != folderPath.ToUpper())
            {
                string previousFolder = folderPath.Substring(0, folderPath.LastIndexOf("\\"));
                backHtml = "<tr class=\"m-row2\"><td colspan=\"5\"><a href=\"Main.aspx?path=" + Server.UrlEncode(previousFolder) + "\"><img src=\"Images/IcoLeft.gif\" alt=\"返回上级\" align=\"absmiddle\" /> 返回上级</a></td></tr>";
            }
            else
            {
                folderPath += "\\";
            }

            // 绑定数据
            fileManage = new FileManagerProcessor();
            List<FileFolderInfo> files = fileManage.GetDirectories(folderPath);

            if (fileManage.Access)
            {
                folderNum = fileManage.FolderNum;
                fileNum = fileManage.FileNum;

                rptList.DataSource = files;
                rptList.DataBind();
            }
            else
            {
                builder.Append("无权限访问该目录. <a href='javascript:history.go(-1);' style='font-weight: normal'>后退</a>");
            }


            if (builder.ToString() != "")
            {
                string builderResult = builder.ToString();
                builder = new System.Text.StringBuilder("");
                builder.AppendFormat("<script type=\"text/javascript\">$(\"#tips\").show(); $(\"#tipsMsg\").html(\"{0}\"); </script>", builderResult.Replace(@"\", @"\\"));
            }
        }
    }
}